from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseForbidden
from django.http import HttpResponseNotAllowed
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.shortcuts import render_to_response 
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from alpha.models import PhoneNumber
from alpha.models import UserProfile
from alpha.models import Activity
from alpha.models import Invite
from alpha.models import MassInvite
from alpha.models import Review
from alpha.models import Feedback
from alpha.models import Prospective

  
@login_required
def stream(request, page=0):
  response = {'is_stream': True, 'page': page}
  page = int(page)
  response['next_page'] = str(page + 1)
  response['prev_page'] = str(page - 1) if page > 0 else None
  if request.user.is_staff:
    start = int(page) * 20
    end = (int(page) + 1) * 20
    if 'username' in request.GET:
      username = request.GET['username']
      response['username'] = username
      response['activities'] = Activity.objects.filter(
          user__username=username).order_by('-created')[start:end]
    else:
      response['activities'] = Activity.objects.all().order_by('-created')[start:end]
    return render_to_response('admin_stream.html', response)
  else:
    return HttpResponseForbidden()

@login_required
def invites(request):
  response = {'is_invite': True}
  if request.user.is_staff:
    if request.method == 'POST':
      if request.POST['command'] == 'new_invite':
        code = User.objects.make_random_password(length=5, 
          allowed_chars='123456789')
        invite = Invite(code=code)
        invite.save()
        redirect('/a/superadmin/invites')
      elif request.POST['command'] == 'new_mass_invite':
        code = User.objects.make_random_password(length=5, 
          allowed_chars='123456789')
        name = request.POST['invite_name'] or 'N/A'
        invite = MassInvite(code=code, name=name)
        invite.save()
        redirect('/a/superadmin/invites')
      elif request.POST['command'] == 'delete':
        invite = Invite.objects.get(pk=request.POST['invite'])
        invite.delete()
      elif request.POST['command'] == 'disable':
        invite = MassInvite.objects.get(pk=request.POST['invite'])
        invite.disabled = not invite.disabled
        invite.save()
    response['invites'] = Invite.objects.all().order_by('used', '-created')
    response['mass_invites'] = MassInvite.objects.all().order_by('-created')
    response.update(csrf(request))
    return render_to_response('admin_invites.html', response)
  else:
    return HttpResponseForbidden()
    
@login_required
def reports(request):
  response = {'is_invite': True}
  if request.user.is_staff:
    response['reports'] = Feedback.objects.all().order_by('-created')
    response.update(csrf(request))
    return render_to_response('admin_report.html', response)
  else:
    return HttpResponseForbidden()
    
@login_required
def prospectives(request):
  response = {'is_prosp': True}
  if request.user.is_staff:
    if request.method == 'POST':
      if request.POST['command'] == 'new_invite':
        prosp = Prospective.objects.get(pk=request.POST['prosp_id'])
        code = User.objects.make_random_password(length=5, 
          allowed_chars='123456789')
        invite = Invite(code=code)
        invite.save()
        prosp.invite = invite
        prosp.save()
    response['prospectives'] = Prospective.objects.all().order_by('-created')
    response.update(csrf(request))
    return render_to_response('admin_prospective.html', response)
  else:
    return HttpResponseForbidden()
    
    
@login_required
def reviews(request):
  response = {'is_review': True}
  if request.user.is_staff:
    response['reviews'] = Review.objects.all().order_by('-date')
    response.update(csrf(request))
    return render_to_response('admin_reviews.html', response)
  else:
    return HttpResponseForbidden()